<!DOCTYPE html >
<html>

<head>
  <meta charset="UTF-8" />
  <title>Hello World</title>
  <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
 
</head>

<body>
  <div id="root"></div>
  <script type="text/babel">
  class Button extends React.Component {
    constructor(props){
      super(props);
      this.state = {
        isEnable: true,
        text:'获取验证码',
        timer: 10
      };
    }
    componentDidMount(){
      console.log(this)
    }
    countDown = () => this.timerId = setInterval(this.tick, 1000)
     tick = (prevState) => {
      //  console.log(this.state.timer)
       
       if(this.state.timer >= 1 ){
         console.log(this.state.timer--)
         this.setState({timer: this.state.timer--})
        // this.setState(prevState => ({timer: prevState.timer--}))
       }else {
          this.timerId && clearInterval(this.timerId)
          this.setState({timer: 10, text: '重新获取', isEnable: true}) 
       }
     } 
     componentWillUnmount(){
       this.timerId && clearInterval(this.timerId)
     }
    click = () => {
      this.countDown()
      this.setState({isEnable: false})
    }
     
    render(){
      return (
        <button disabled={!this.state.isEnable} onClick={this.click} >{this.state.text} {this.state.isEnable ? '': this.state.timer}</button>
      );
    }
  }
  class Box extends React.Component {
    constructor(props){
      super(props);
    }
    render(){
        return (
          <div>
            <input type="text" placeholder="please input code" />
            <Button />
            </div>
        );
      };
  }
  ReactDOM.render(
    <Box />,
    document.getElementById('root')
  );
  </script>
  <!--
          Note: this page is a great way to try React but it's not suitable for production.
          It slowly compiles JSX with Babel in the browser and uses a large development build of React.
    
          To set up a production-ready React build environment, follow these instructions:
          * https://reactjs.org/docs/add-react-to-a-new-app.html
          * https://reactjs.org/docs/add-react-to-an-existing-app.html
    
          You can also use React without JSX, in which case you can remove Babel:
          * https://reactjs.org/docs/react-without-jsx.html
          * https://reactjs.org/docs/cdn-links.html
        -->
</body>

</html>